package com.wenyu.wenyuyuedu.adapter.homepagefragment;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.wenyu.wenyuyuedu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/25.
 * 帖子详情的头布局中gridview的点击事件的viewpager的adapter
 */

public class GrideViewViewpageAdapter extends PagerAdapter {
    private Activity context;
    private List<String> imageviews;

    public GrideViewViewpageAdapter(Activity context, ArrayList<String> imageviews) {
        this.context = context;

        this.imageviews = imageviews;
    }

    @Override
    public int getCount() {
        return imageviews.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.photo_big_item, container, false);
        PhotoView photo_view = (PhotoView) view.findViewById(R.id.photo_view);
        photo_view.enable();
        final ProgressBar photo_pb = (ProgressBar) view.findViewById(R.id.photos_pb);
        Glide.with(context)
                .load(imageviews.get(position))
                .thumbnail(0.1f)
                .error(R.drawable.login_logo)
                .crossFade()
                .into(new GlideDrawableImageViewTarget(photo_view) {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                        super.onResourceReady(resource, animation);
                        photo_pb.setVisibility(View.GONE);
                    }
                });

        container.addView(view);
        return view;
    }
}
